<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\models\Goods;
use app\models\Play;
use app\models\Stock;
use yii;


class GoodsLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getGoods($query = [], $page = 1, $limit = 10)
    {
        $goods = Goods::find()
            ->select('g.*,p.play_name')
            ->from('{{%goods}} g')
            ->leftJoin('{{%play}} p','p.play_id=g.play_id')
            ->filterWhere($query)->orderBy('goods_id desc')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $goods->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $goods->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'goods/index',
                'query' => $query
            ]
        ];
        return $return;
    }



    public function editById($id, $params, $uploadresult)
    {
        if(!$goods = Goods::findOne($id)) return false;

        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $goods->goods_name = $params['goods_name'];
        $goods->summary = $params['summary'];
        $goods->price = $params['price'];
        $play = Play::findOne(['play_id'=>$params['play_id']]);
        $goods->recovery_num = $play->num;
        if($uploadresult)
            $goods->goods_pic = $uploadresult;
        $goods->goods_pics = $params['goods_pics'];
        $goods->effective_days = $params['effective_days'];
        $goods->age = $params['age'];
        $goods->model = $params['model'];
        $goods->description = $params['description'];
        $goods->play_id = $params['play_id'];
        $result1 = $goods->save();
        //var_dump($result1);exit;
        $result2  = true;
        $stocks = Stock::findAll(['goods_id'=>$id]);
        if($stocks){
            foreach ($stocks AS $ke=>$va){
                $va->delete();
            }
        }
        if($params['stockids']){
            $arr = explode('|',$params['stockids']);
            foreach ($arr AS $key=>$value){
                $item = explode(',',$value);
                $stock = new Stock();
                $stock->goods_id = $goods->goods_id;
                $stock->province_id = $item[0];
                $stock->city_id = $item[1];
                $stock->district_id = $item[2];
                $stock->street_id = $item[3];
                $result2 &= $stock->save();
            }
        }
        if($result1 && $result2){
            $transaction->commit();
            return true;
        }else{
            $transaction->rollBack();
            return false;
        }
    }


    public function getById($id)
    {
        return Goods::find()->where(['goods_id' => $id])->asArray()->one();
    }


    public function saveStatus($id)
    {
        $goods = Goods::findOne($id);
        if($goods->status)
            $goods->status = 0;
        else
            $goods->status = 1;
        return $goods->save();
    }

    public function addGoods($params,$uploadresult){
        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $goods = new Goods();
        $goods->goods_name = $params['goods_name'];
        $goods->summary = $params['summary'];
        $goods->price = $params['price'];
        $play = Play::findOne(['play_id'=>$params['play_id']]);
        $goods->recovery_num = $play->num;
        $goods->goods_pic = $uploadresult;
        $goods->goods_pics = $params['goods_pics'];
        $goods->effective_days = $params['effective_days'];
        $goods->age = $params['age'];
        $goods->model = $params['model'];
        $goods->description = $params['description'];
        $goods->play_id = $params['play_id'];
        $result1 = $goods->save();
        $result2  = true;
        if($params['stockids']){
            $arr = explode('|',$params['stockids']);
            foreach ($arr AS $key=>$value){
                $item = explode(',',$value);
                $stock = new Stock();
                $stock->goods_id = $goods->goods_id;
                $stock->province_id = $item[0];
                $stock->city_id = $item[1];
                $stock->district_id = $item[2];
                $stock->street_id = $item[3];
                $result2 &= $stock->save();
            }
        }
        if($result1 && $result2){
            $transaction->commit();
            return true;
        }else{
            $transaction->rollBack();
            return false;
        }

    }

    public function getStock($goods_id){
        return Stock::find()
            ->select('s.*,a1.region_name as province_name,a2.region_name as city_name,a3.region_name as district_name,a4.region_name as street_name,')
            ->from('{{%stock}} s')
            ->leftJoin('{{%area}} a1','a1.region_code=s.province_id')
            ->leftJoin('{{%area}} a2','a2.region_code=s.city_id')
            ->leftJoin('{{%area}} a3','a3.region_code=s.district_id')
            ->leftJoin('{{%area}} a4','a4.region_code=s.street_id')
            ->where(['goods_id'=>$goods_id])->asArray()->all();
    }




}